import { WeaLeftTree } from 'ecCom';
import { toJS } from 'mobx';
import { inject, observer } from 'mobx-react';

@inject('coworkMainStore')
@observer
class CoworkTree extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			treeCounts: {},
			treeDatas: [],
			treeTypes: [],
		};
	}
	componentDidMount() {
	  const { coworkMainStore } = this.props;
    coworkMainStore.getCoworkTreeDatas();
  }

  onSearchLabelClick = () => {
    const { coworkMainStore } = this.props;
    coworkMainStore.setSelectedTreeKeys([]);
    coworkMainStore.doFormReset();
    coworkMainStore.reloadLeftList();
    coworkMainStore.onTreeSelect();
  }
	render() {
		const { coworkMainStore } = this.props,
treeList = toJS(coworkMainStore.treeList);
		const { selectedTreeKeys } = coworkMainStore;
		const { treeCounts, treeDatas, treeTypes } = treeList;
		return (
			<div style={{ height: '100%' }}>
        <WeaLeftTree ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftTree@6kwtcf`}
          datas={treeDatas}
          counts={treeCounts}
          countsType={treeTypes}
          onSelect={this.onSelect}
          selectedKeys={toJS(selectedTreeKeys)}
          onSearchLabelClick={this.onSearchLabelClick}
        />
			</div>
		);
	}

	onSelect = (str) => {
		if (!str) return;
		const { coworkMainStore } = this.props;
		let mainTypeId = '',
			typeId = '';
		if (str.indexOf('cMainType_') >= 0) {
			mainTypeId = str.substr(10);
		} else if (str.indexOf('cType_') >= 0) {
			typeId = str.substr(6);
    }
		coworkMainStore.onTreeSelect(typeId, mainTypeId);
	}
}
export default CoworkTree;
